#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random
import time
from requests import get
import json
import datetime

# user_agent列表
user_agent_list = [
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)',
    'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36'
]

# referer列表
referer_list = [
    'http://fund.eastmoney.com/110022.html',
    'http://fund.eastmoney.com/110023.html',
    'http://fund.eastmoney.com/',
    'http://fund.eastmoney.com/110025.html'
]


def get_html(baseUrl):
    # 获取一个随机user_agent和Referer
    headers = {'User-Agent': random.choice(user_agent_list), 'Referer': random.choice(referer_list)}
    try:
        resp = get(baseUrl, headers=headers)
        # print(resp.status_code)
        if resp.status_code == 200:
            # print(resp.content)
            return resp.text
        print("没有爬取到相应的内容")
        return None
    except RequestException:
        print("没有爬取到相应的内容")
        return None



def getdate(beforeOfDay):
    today = datetime.datetime.now()
    # 计算偏移量
    offset = datetime.timedelta(days=-beforeOfDay)
    # 获取想要的日期的时间
    re_date = (today + offset).strftime('%Y-%m-%d')
    return re_date

    
t0 = 'INSERT INTO "WB_TBL_DD_LME_PRICE"("ID", "VARIETY_ID", "M3_BUY", "M27_BUY", "M27_SELL", "M15_BUY", "M15_SELL", "M3_SELL", "CASH_BUY", "CASH_SELL", "USING_FLAG", "GROUP_ID", "ORGAN_ID", "ORGAN_LEVEL_CODE", "DELETE_FLAG", "CREATE_USER", "CREATE_DATE", "UPDATE_USER", "UPDATE_DATE", "VERSION_NUM", "TRADE_DATE", "CONTRACT_ID", "DATA_SOURCE", "OPEN_PRICE", "CLOSE_PRICE", "MAX_PRICE", "MIN_PRICE", "SETTLEMENT_PRICE") VALUES (WB_SEQ_DD_LME_PRICE.nextval'
t1 = ", 'Cu', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '1', NULL, NULL, NULL, '0', '获取用户信息为空', SYSDATE, '获取用户信息为空', SYSDATE, '1', "
t2 = " '0');"



if __name__ == "__main__":

    t = time.time()
    rt = int(round(t * 1000))

    baseUrl = "http://api.fund.eastmoney.com/f10/lsjz?fundCode=004070&pageIndex=1&pageSize=100&startDate=&endDate=&_=" + str(rt)
    # print(baseUrl)
    data = get_html(baseUrl)
    # print(data)

    jsonText = json.loads(data)
    infos = jsonText['Data']['LSJZList']

    r = ''
    for info in infos:
        # print(info)
        TRADE_DATE = info['FSRQ']     # 日期
        DWJZ = float(info['DWJZ']) * 4666    # 单位净值
        LJJZ = info['LJJZ']     # 累计净值
        JZZZL = info['JZZZL']     # 增长率

        p3 = DWJZ + random.randint(0,200)
        p4 = DWJZ - random.randint(0,200)
        # print("p3:{};p4:{}".format(p3,p4))
        p2 = random.randint(int(p4),int(p3))
        p1 = random.randint(int(p4),int(p3))

        # print(str(p1))
        # print(str(p2))
        # print(str(p3))
        # print(str(p4))      
        # print(type(TRADE_DATE))

        r += t0 + t1 + "TO_DATE('" + TRADE_DATE + "', 'YYYY-MM-DD'), '8', '0', " + "'" + str(p1) + "', "  + "'" + str(p2) + "', " + "'" + str(p3) + "', " + "'" + str(p4) + "', " + t2 + "\n"
        # r += str(t) + str(t1) + "TO_DATE('" + str(TRADE_DATE) + "', 'YYYY-MM-DD'), '8', '0', " + "'"  + str(p1) + "', "  + "'" + str(p2) + "', " + "'" + str(p3) + "', " + "'" + str(p4) + "', " + t2 + "\n"

    print(r)